<script lang="ts" setup>
import { ref } from 'vue';

interface NavItem {
  id: string;
  name: string;
  icon?: string;
}

interface Props {
  navItems: NavItem[];
  activeNav: string;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (e: 'nav-change', navId: string): void;
  (e: 'open-settings'): void;
}>();

const isCollapsed = ref(false);

const handleNavClick = (navId: string) => {
  emit('nav-change', navId);
};

const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value;
};

const openSettings = () => {
  emit('open-settings');
};
</script>

<template>
  <div class="sidebar" :class="{ collapsed: isCollapsed }">
    <div class="sidebar-header">
      <h2 v-if="!isCollapsed">网站导航</h2>
      <button class="settings-btn" v-if="!isCollapsed" @click="openSettings">⚙️</button>
      <button class="toggle-btn" @click="toggleSidebar">
        {{ isCollapsed ? '▶' : '◀' }}
      </button>
    </div>
    <nav class="nav-menu">
      <ul>
        <li 
          v-for="item in navItems" 
          :key="item.id"
          :class="{ active: activeNav === item.id }"
          @click="handleNavClick(item.id)"
        >
          <span class="nav-icon">{{ item.icon }}</span>
          <span v-if="!isCollapsed" class="nav-name">{{ item.name }}</span>
        </li>
      </ul>
    </nav>
  </div>
</template>

<style scoped>
.sidebar {
  width: 220px;
  background: #2c3e50;
  color: white;
  height: 100vh;
  overflow-y: auto;
  box-shadow: 2px 0 5px rgba(0,0,0,0.1);
  transition: width 0.3s ease;
  position: relative;
}

.sidebar.collapsed {
  width: 60px;
}

.sidebar-header {
  padding: 20px;
  border-bottom: 1px solid #34495e;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
}

.settings-btn {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 5px;
  border-radius: 3px;
  color: white;
}

.settings-btn:hover {
  background-color: #34495e;
}

.toggle-btn {
  background: none;
  border: none;
  color: white;
  font-size: 16px;
  cursor: pointer;
  padding: 5px;
  border-radius: 3px;
}

.toggle-btn:hover {
  background-color: #34495e;
}

.nav-menu ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-menu li {
  padding: 15px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: background-color 0.2s;
  white-space: nowrap;
  overflow: hidden;
}

.nav-menu li:hover {
  background-color: #34495e;
}

.nav-menu li.active {
  background-color: #3498db;
}

.nav-icon {
  margin-right: 10px;
  font-size: 16px;
  min-width: 20px;
  text-align: center;
}

.nav-name {
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    height: auto;
  }
  
  .sidebar.collapsed {
    width: 100%;
  }
  
  .nav-menu li {
    display: inline-block;
    padding: 10px 15px;
  }
}
</style>